<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // var rob = function (nums) {
    //   var pre = 0, cur = 0, i = 0, l = nums.length;
    //   while (i < l) {
    //     var m = pre;
    //     pre = Math.max(pre, cur)
    //     cur = m + nums[i++];
    //   }
    //   return Math.max(pre, cur)
    // };
    var rob = function (nums) {
      return Math.max(...nums.reduce((dp, num) => [Math.max(...dp), dp[0] + num], [0, 0]))
    };
    console.log(rob([1, 2, 3, 1]));
  </script>
</body>

</html>